package com.basswold.nasaesa.client;
import com.basswold.nasaesa.client.ConstructionModule.ModuleType;

/**
 * Used to build a control wing of a mars base.
 * A control wing contains control, power, airlock, and medical modules.
 * @author Keith
 *
 */
public class ControlWingFactory extends BuildingWingFactory {	
	/**
	 * Constructor.
	 * @param moduleList Lists structure controlling access to modules.
	 */
	public ControlWingFactory(final ContructionList moduleList) {
		super(moduleList);
	}

	@Override
	public final ConstructionConnectorModule startConstruction(
			final ConstructionModule.CardinalDirection direction, boolean airlockIncluded) {
		
		ConstructionConnectorModule connector = null;
		
		try {
			// Build an airlock with a medical module next to it.
			if (airlockIncluded 
					&& modules.hasModuleOfType(ModuleType.AIRLOCK)
					&& modules.hasModuleOfType(ModuleType.PLAIN)) {
				connector = (ConstructionConnectorModule) modules.getModuleOfType(
						ConstructionModule.ModuleType.PLAIN);
				this.setEndConnection(connector, 
						modules.getModuleOfType(ModuleType.AIRLOCK), direction);
				// Add a medical module if possible
				connector = this.addModule(connector, 
						modules.getModuleOfType(ModuleType.MEDICAL), direction);
			}
			else {
				// set the end module to be anything acceptable for this wing
				connector = (ConstructionConnectorModule) modules.getModuleOfType(
						ConstructionModule.ModuleType.PLAIN);
				ConstructionModule[] array = this.getModule(1);
				if (array[0] != null) {
					this.setEndConnection(connector, array[0], direction);
				} else {
					//array way null, nothing can be added
					modules.addModule(connector);
					connector = null;
				}
			}
			
		} catch (Exception Ex) {
			// Should never get here, we are making sure that all modules we
			// are using exist prior to using them.
			return null;
		}
		
		return connector;
	}

	@Override
	protected final ModuleType[] getAcceptableModules() {
		return new ConstructionModule.ModuleType[]
            {ConstructionModule.ModuleType.POWER, 
  			 ConstructionModule.ModuleType.CONTROL,
  			 ConstructionModule.ModuleType.MEDICAL};
	}

	@Override
	public FactoryType getType() {
		return FactoryType.ControlWingFactory;
	}
}
